// packageA/member/supplier/hotSearch/hotSearch.js
const app = getApp();
// const GOOD_URL = "plugin.supplier.frontend.shop.index.search-goods";
Page({

  /**
     * 页面的初始数据
     */
  data: {
    showcccc: false,
    inputs: '',
    good_page: 1,
    order_field: '',
    order_by: '',
    is_hot:0,
    cid:'',
    debtId:''
  },

  /**
     * 生命周期函数--监听页面加载
     */
  onLoad: function (options) {
    
    if (options.id) {
      this.setData({
        id: options.id
      });
    }
    if(options.hot == 1){
      this.setData({
        is_hot:options.hot
      });
    }
    if(options.cid){
      this.setData({
        cid:options.cid
      });
      wx.setNavigationBarTitle({
        title: '供应商店铺',
      });
    }
    if(options.debtCId){
      this.setData({
        debtCId:options.debtCId,
        is_debt:true
      })
      wx.setNavigationBarTitle({
        title: '分类',
      });
    }
    if(options.debtId){
        this.setData({
            debtId:options.debtId
        })
    }
    if(options.debtSearch){
      this.setData({
        is_debt:true
      })
      wx.setNavigationBarTitle({
        title: '搜索',
      });
    }
    console.log(options,'xxxxxxx')
    this.getGoodsData();
  },

  /**
     * 生命周期函数--监听页面初次渲染完成
     */
  onReady: function () {
    let language = wx.getStorageSync('langIndex');
    this.setData({
      'language': language.en
    });
  },
  goToDetail(e){
    let id = e.currentTarget.dataset.id;
    if(this.data.debtId){
        wx.navigateTo({
            url: '/packageA/detail_v2/detail_v2?id='+id+'&debtId='+this.data.debtId,
          })
    }else{
        wx.navigateTo({
            url: '/packageA/detail_v2/detail_v2?id='+id,
        })
    }
  },
  /**
     * 生命周期函数--监听页面显示
     */
  onShow: function () {

  },

  /**
     * 生命周期函数--监听页面隐藏
     */
  onHide: function () {

  },

  /**
     * 生命周期函数--监听页面卸载
     */
  onUnload: function () {

  },

  /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
  onPullDownRefresh: function () {

  },

  /**
     * 页面上拉触底事件的处理函数
     */
  onReachBottom: function () {
    if(this.data.current_page >= this.data.good_total_page){
      return;
    }else{
      this.setData({
        good_page : this.data.good_page + 1
      });
      this.nexTgetGoodsData();
    }
  },

  /**
     * 用户点击右上角分享
     */
  onShareAppMessage: function () {

  },
  classifyBolbtn() {
    if (this.data.showcccc) {
      this.setData({
        showcccc: false,
      });
    } else {
      this.setData({
        showcccc: true,
      });
    }
  },
  inpbtn(e) {
    let val = e.detail.value;
    this.setData({
      inputs: val
    });
  },
  search() {
    this.setData({
      good_page:1,
    });
    // 搜索
    this.getGoodsData();
  },
  toggle(e) {
    let idx = e.currentTarget.dataset.idx;
    let order_field = '';
    switch (idx) {
    case 2:
      this.setData({
        hascolor: 2
      });
      if (this.data.colorshow == '2_2') {
        this.setData({
          colorshow: '2_1',
        });
        order_field = 'show_sales';
        this.setData({
          order_field: order_field,
          order_by: 'asc'
        });
      } else {
        this.setData({
          colorshow: '2_2',
        });
        order_field = 'show_sales';
        this.setData({
          order_field: order_field,
          order_by: 'desc'
        });
      }
      break;
    case 3:
      this.setData({
        hascolor: 3,
      });
      if (this.data.colorshow == '3_2') {
        this.setData({
          colorshow: '3_1'
        });
        order_field = 'price';
        this.setData({
          order_field: order_field,
          order_by: 'asc'
        });
      } else {
        this.setData({
          colorshow: '3_2'
        });
        order_field = 'price';
        this.setData({
          order_field: order_field,
          order_by: 'desc'
        });
      }
      break;
    case 5:
      this.setData({
        hascolor: 5,
      });
      if (this.data.colorshow == '5_2') {
        this.setData({
          colorshow: '5_1'
        });
        order_field = 'id';
        this.setData({
          order_field: order_field,
          order_by: 'asc'
        });
      } else {
        this.setData({
          colorshow: '5_2'
        });
        order_field = 'id';
        this.setData({
          order_field: order_field,
          order_by: 'desc'
        });
      }
      break;
    }
    this.setData({
      good_page:1
    })
    this.getGoodsData();
  },
  tapUpper() {
    try {
      wx.navigateBack({
        delta: 1,
        success: (res) => {},
        fail: (res) => {
          wx.reLaunch({
            url: '/packageG/index/index'
          });
        },
        complete: (res) => {},
      });
    } catch (error) {
      wx.reLaunch({
        url: '/packageG/index/index'
      });
    }
  },
  getGoodsData() {
    let that = this;
    let urlStr = '';
    if(this.data.is_debt){
      urlStr = app.getNetAddresss("goods.goods.search-goods");
      if(!app._isTextEmpty(this.data.debtCId)){
        urlStr += `&search[category]=`+this.data.debtCId;
      }
    }else{
      urlStr = app.getNetAddresss("plugin.supplier.frontend.shop.index.search-goods");
      urlStr += '&sid=' + this.data.id;
      if(!app._isTextEmpty(this.data.cid)){
        urlStr += `&category_id=`+this.data.cid;
      }
    }
    urlStr += '&page=' + this.data.good_page;
    urlStr += '&order_field=' + this.data.order_field;
    urlStr += '&order_by=' + this.data.order_by;
    urlStr += `&search[keyword]=${this.data.inputs || ''}`;
    if(this.data.is_hot == 1){
      urlStr +=`&is_hot=1`;
    }
    
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            goodsArr: res.data.data,
            good_total_page: res.data.last_page,
            current_page: res.data.current_page
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      }
    });
  },
  nexTgetGoodsData() {
    let urlStr = '';
    if(this.data.is_debt){
      urlStr = app.getNetAddresss("goods.goods.search-goods");
      if(!app._isTextEmpty(this.data.debtCId)){
        urlStr += `&search[category]=`+this.data.debtCId;
      }
    }else{
      urlStr = app.getNetAddresss("plugin.supplier.frontend.shop.index.search-goods");
      urlStr += '&sid=' + this.data.id;
      if(!app._isTextEmpty(this.data.cid)){
        urlStr += `&category_id=`+this.data.cid;
      }
    }
    urlStr += '&page=' + this.data.good_page;
    urlStr += '&order_field=' + this.data.order_field;
    urlStr += '&order_by=' + this.data.order_by;
    urlStr += `&search[keyword]=${this.data.inputs || ''}`;
    if(this.data.is_hot == 1){
      urlStr +=`&is_hot=1`;
    }
    app._getNetWork({
      url: urlStr,
      success: ((resdata)=>{
        var res = resdata.data;
        if (res.result == 1) {
          this.setData({
            goodsArr: this.data.goodsArr.concat(res.data.data),
            good_total_page: res.data.last_page,
            current_page: res.data.current_page
          });
        }
      }),
      fail: ((res)=>{
        wx.showToast({
          title: res.msg,
        });
      })
    });
  }
});
